﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using QuanLyHocBongSinhVien.VO;
using QuanLyHocBongSinhVien.DAO;
using QuanLyHocBongSinhVien.Bus;


namespace QuanLyHocBongSinhVien
{
    public partial class frmLoaiHocBong_NamHoc : Form
    {
        LoaiHocBong_Bus lhbb = new LoaiHocBong_Bus();
        NamHoc_Bus nhb = new NamHoc_Bus();
        BindingSource bs = new BindingSource();
        DataTable dt = new DataTable();
        LoaiHB_NamHoc_Bus lhbnh = new LoaiHB_NamHoc_Bus();
        public frmLoaiHocBong_NamHoc()
        {
            InitializeComponent();
        }

        private void frmLoaiHocBong_NamHoc_Load(object sender, EventArgs e)
        {
            txtMaNH.DataSource = nhb.getMaNH1();
            txtMaNH.DisplayMember = "MANAMHOC";
            txtMaNH.ValueMember = "MANAMHOC";
            txtMaHB.DataSource = lhbb.getMaHB1();
            txtMaHB.DisplayMember = "MaHB";
            txtMaHB.ValueMember = "MaHB";

            dt = lhbnh.GetAll();
            bs.DataSource = dt;
            dgvHBNH.DataSource = lhbnh.GetAll();
        }

        private void dgvHBNH_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát không ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            txtGhiChu.Clear();
        }

        public bool check_id(string id)
        {

            dbConnection db = new dbConnection();
            SqlParameter mabomon = new SqlParameter("@mahb", id);
            DataTable dt = db.ExecuteQuery("SELECT * FROM LOAIHOCBONG WHERE MAHB = @mahb", mabomon);
            return dt.Rows.Count > 0;
        }
        private void btnSua_Click(object sender, EventArgs e)
        {
            lhbnh.update(txtMaNH.Text, txtMaHB.Text, txtGhiChu.Text);
            dgvHBNH.DataSource = lhbnh.GetAll();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (check_id(txtMaHB.Text))
            {
                MessageBox.Show("Bạn Đã Nhập Trùng Thông Tin");
                return;
            }

            if (txtMaHB.Text == "")
            {
                MessageBox.Show("Bạn Chưa Nhập Mã");
                return;
            }
            else
            {
                lhbnh.add(txtMaNH.Text, txtMaHB.Text, txtGhiChu.Text);
                dgvHBNH.DataSource = lhbnh.GetAll();
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            lhbnh.delete(txtMaNH.Text);
             dgvHBNH.DataSource = lhbnh.GetAll(); 
        }

       
    }
}
